﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;
using ASER.DataAccess.BaseDataAccess;
using ASER.DataEntity.Security;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace ASER.DataAccess.Security
{
    public class LoginDataAccess : BaseDataAccess.BaseDataAccess<SysOperatorEntity>
    {
        public List<SysMenuEntity> GetMenus(ref SysOperatorEntity user)
        {
            string SqlText = "SELECT * FROM  Sys_Operator WHERE UserName=@UserName AND Password = @Password ";

            Database db = DatabaseFactory.CreateDatabase("Platform");
            using (DbCommand Command = db.GetSqlStringCommand(SqlText))
            {
                db.AddInParameter(Command, "@UserName", DbType.String, user.UserName);
                db.AddInParameter(Command, "@Password", DbType.String, user.Password);
                IDataReader Reader = db.ExecuteReader(Command);
                try
                {
                    user = FillEntities(Reader);
                }
                finally
                {
                    Reader.Close();
                    Reader.Dispose();
                }
            }
            if (user.UserCode == "" || user.UserCode == null)
                return null;

            return new SysmenuDataAccess().GetMenus(user);
        }

        public bool VerificationUser(string userCode, string password)
        {
            string SqlText = "SELECT count(*) FROM  Sys_Operator WHERE UserName=@UserCode AND Password = @Password ";

            Database db = DatabaseFactory.CreateDatabase("Platform");
            using (DbCommand Command = db.GetSqlStringCommand(SqlText))
            {
                db.AddInParameter(Command, "@UserCode", DbType.String, userCode);
                db.AddInParameter(Command, "@Password", DbType.String, password);
                int res = Convert.ToInt32(db.ExecuteScalar(Command));
                if (res == 1)
                    return true;
                return false;
            }
           
        }

        public SysOperatorEntity GetUserData(string userCode)
        {
            string SqlText = "SELECT * FROM  Sys_Operator WHERE UserName=@UserCode";
            SysOperatorEntity user  = null;
            Database db = DatabaseFactory.CreateDatabase("Platform");
            using (DbCommand Command = db.GetSqlStringCommand(SqlText))
            {
                db.AddInParameter(Command, "@UserCode", DbType.String, userCode);
                IDataReader Reader = db.ExecuteReader(Command);
                try
                {
                   user = FillEntities(Reader);
                }
                finally
                {
                    Reader.Close();
                    Reader.Dispose();
                }
            }
            return user;
        }

        public List<SysMenuEntity> GetUserPermits(string userCode)
        {
            SysOperatorEntity user = new SysOperatorEntity();
            user.UserCode = userCode;
            return new SysmenuDataAccess().GetMenus(user);
        }

        public List<DeptartmentEntity> GetDepts()
        {
            return new DeptDataAccess().GetDepts();
        }

        public List<SysOperatorEntity> GetOperators(string orgCode)
        {
            string SqlText = "SELECT * FROM  Sys_Operator WHERE OrgCode = @OrgCode ";

            List<SysOperatorEntity> Result = null;
            Database db = DatabaseFactory.CreateDatabase("Platform");
            using (DbCommand Command = db.GetSqlStringCommand(SqlText))
            {
                db.AddInParameter(Command, "@OrgCode", DbType.String, orgCode);
                IDataReader Reader = db.ExecuteReader(Command);
                try
                {
                    Result = FillDataListGeneric(Reader);
                }
                finally
                {
                    Reader.Close();
                    Reader.Dispose();
                }
            }
            return Result;
        }
    }
}
